<template>
  <div>
    <el-card shadow="never">
      <header-title title="抓拍记录"></header-title>
      <p>
        <el-date-picker
          format="yyyy/MM/dd"
          value-format="yyyy/MM/dd"
          v-model="selectDate"
          :value="selectDate"
          @change="changeDate" type="daterange"
          range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
        <el-button @click="search" class="button" size="small" round icon="el-icon-search">查询</el-button>
      </p>
    </el-card>
    <el-card shadow="never">
      <div>
        <el-table :data="tableData" v-loading="loading" style="width: 100%">
          <el-table-column prop="originalFaceUrl" label="人脸">
            <template slot-scope="scope">
              <img :src="scope.row.originalFaceUrl" alt="" style="height:3rem;">
            </template>
          </el-table-column>
          <el-table-column prop="createdAt" label="时间"></el-table-column>
          <el-table-column prop="age" label="年龄"></el-table-column>
          <el-table-column prop="gender" label="性别"></el-table-column>
          <el-table-column prop="deviceName" label="设备"></el-table-column>
          <div slot="empty"><img src="../../../assets/blank_tip_page/blank_tip_page.png" alt=""></div>
        </el-table>
        <el-pagination
          :page-size="perPage"
          :current-page="page"
          :total="total"
          @current-change="changePage"
          style="margin-top: 15px;"
          background
          layout="prev, pager, next"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import HeaderTitle from "@/components/HeaderTitle"
import { isNil, formatDate, formatDate2 } from "@/utils"

let startDateInit = formatDate2(new Date())
let endDateInit = formatDate2(new Date())

export default {
  name: 'captureRecords',
  components: {
     HeaderTitle
  },
  data(){
    return {
      loading: true,
      modal: {
        startDate: formatDate2(new Date()),
        endDate: formatDate2(new Date())
      },
      selectDate: [startDateInit, endDateInit],
      tableData: [],
      perPage: 10, // 每页显示10条
      page: 1, // 当前页
      total: 0
    }
  },
  watch: {
    '$store.state.app.marketId'(val, oldVal){
      this.getData(this.modal.startDate,this.modal.endDate,this.perPage,this.page)
    }
  },
  mounted(){
    this.getData(this.modal.startDate,this.modal.endDate,this.perPage,this.page)
  },
  methods:{
    // 获取数据
    getData(startDate,endDate,perPage,page){
      this.$store.dispatch('peopleNumber/fetchCaptureRecordsData',{
        startDate,endDate,perPage,page
      }).then(
        data => {
          this.loading = false
          for (let i = 0; i < data.items.length; i++) {
            data.items[i].gender = (data.items[i].gender === 1 ? '男' : '女')
          }
          this.tableData = data.items
          this.total = data.total
        }
      ).catch(err => {
        this.loading = false
        this.tableData = []
        this.total = 0
      })
    },
    changePage(num){
      this.page = num
      this.getData(this.modal.startDate,this.modal.endDate,this.perPage,num)
    },
    search(){
      this.page = 1
      this.getData(this.modal.startDate,this.modal.endDate,this.perPage,this.page)
    },
    changeDate(date){
      this.modal.startDate = formatDate2(date[0])
      this.modal.endDate = formatDate2(date[1])
    }
  }
}
</script>

<style lang="scss" scoped>
  /deep/ .el-table__empty-text{
    width: 100%;
  }
.button {
  background: #64d9d6;
  color: #fff;
  margin-left: 40px;
}
</style>
